import java.util.HashMap;
import java.util.Map;

public class Solution1658 {
    public int minOperations(int[] nums, int x) {
        Map<Long,Integer> map=new HashMap<>();
        long a=0;
        for (int i=0;i<nums.length;i++){
            a+=nums[i];
            if (!map.containsKey(a)){
                map.put(a,i);
            }
        }
        int ans=Integer.MAX_VALUE;
        if (map.containsKey((long)x)){
            ans=map.get((long)x)+1;
        }
        long t=0;
        for (int i=nums.length-1;i>=0;i--){
            t+=nums[i];
            if (map.containsKey((long)x-t)){
                ans=Math.min(ans,nums.length-i+map.get((long)x-t)+1);
            }
            if (t==x){
                ans=Math.min(ans,nums.length-i);
            }
        }
        return ans>nums.length?-1:ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1658().minOperations(new int[]{1,1,4,2,3},5));
    }
}
